import { useEffect } from "react";
import { useLatest } from "./use-latest";
import { TypeOf } from "./type-of";

const useUnmount = (fn: any): void => {
  if (!TypeOf.isFunction(fn)) {
    console.warn(
      `useUnmount expected parameter is a function, got ${typeof fn}`
    );
  }

  const fnRef = useLatest(fn);

  useEffect(
    () => () => {
      fnRef.current();
    },
    []
  );
};

export { useUnmount };
